/*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 ("License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.zimbra.com/license
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is: Zimbra AJAX Toolkit.
*
* The Initial Developer of the Original Code is Zimbra, Inc.
* Portions created by Zimbra are Copyright (C) 2005 Zimbra, Inc.
* All Rights Reserved.
*
* Portions of the original code have been adapted for CLDC 1.0 and MIDP 2.0 by CalemEAM Inc. 
* Portions created by CalemEAM are Copyright (C) 2008 CalemEAM Inc. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK *****
*/

package com.calemeam.text;

import java.util.Hashtable;
import java.util.Vector;

// NOTE: The API for the classes in this file are inspired by the Java text
//		 formatting classes but the implementation was NOT copied or ported
//		 from the Java sources.

/** 
 * Base class for all formats. To format an object, instantiate the
 * format of your choice and call the <code>format</code> method which
 * returns the formatted string.
 */
public class MicroFormat {
   
   /**
    * Cached formatters
    */
   protected static Hashtable FORMATTERS = new Hashtable();
   
   protected String pattern;
   protected Vector segments;
   
   public MicroFormat(String pattern) {
      this.pattern=pattern;
      this.segments=new Vector();
   }
   
   /** Returns string representation of this object. */
   //#if Debug
//#    public String toString() {
//#       StringBuffer sb=new StringBuffer();
//#       sb.append("pattern=").append(this.pattern);
//#       if (this.segments.size()>0) {
//#          sb.append("; segments={ ");
//#          for (int i=0; i< this.segments.size(); i++) {
//#             sb.append(this.getSegment(i)).append("; ");
//#          }
//#          sb.append("}");
//#       }
//#       return sb.toString();
//#    }
   //#endif

   // Public methods

   /** 
    * This method does <em>not</em> need to be overridden unless
    * the subclass doesn't use format segments and takes complete 
    * responsibility for formatting.
    */
   protected String _format(Object ob) {
      StringBuffer sb=new StringBuffer();
      for (int i=0; i< this.segments.size(); i++) {
         sb.append(this.getSegment(i).format(ob));
      }
      return sb.toString();
   }

   /** 
    * Parses the given string according to this format's pattern and returns
    * an object.
    * <p>
    * <strong>Note:</strong>
    * The default implementation of this method assumes that the sub-class
    * has implemented the <code>_createParseObject</code> method.
    */
   //#if ParsingConfiguration
//#    public Object parseObj(String s) throws MicroParsingException {
//#       Object ob=this.createParseObject();
//#       int index=0;
//#       for (int i=0; i< this.segments.size(); i++) {
//#          index=this.getSegment(i).parse(ob, s, index);
//#       }
//#       return ob;
//#    }
   //#endif

   /** 
    * Returns an array of segments that comprise this format. 
    * <p>
    * <strong>Note:</strong>
    * This method is specific to this implementation and does not follow
    * the format classes found in the <code>java.text</code> package.
    */
   public Vector getSegments() {
      return this.segments;
   }
   //#if Debug
//#    public String getPattern() {
//#       return this.pattern;
//#    }
   //#endif
   public MicroSegment getSegment(int i) {
      return (MicroSegment)this.segments.elementAt(i);
   }
   
   // Protected methods

   /**
    * Creates the object that is initialized by parsing
    * <p>
    * <strong>Note:</strong>
    * This must be implemented by sub-classes.
    */
   //#if ParsingConfiguration
//#    public Object createParseObject() {
//#       return null;
//#    }
   //#endif

   //static methods
   public static String zeroPad(int i, int l) {
      return zeroPad(Integer.toString(i), l);
   }
   public static String zeroPad(String p, String s) {
      return zeroPad(p, s.length());
   }
   public static String zeroPad(int i, String s) {
      return zeroPad(Integer.toString(i), s.length());
   }
   public static String zeroPad(String s, int length) {
      return zeroPad(s, length, "0", false);
   }
   public static String zeroPad(String s, int length, boolean bl) {
      return zeroPad(s, length, "0", bl);
   }
   public static String zeroPad(String s, int length, String zeroChar, boolean rightSide) {
      if (s.length() >= length) return s;
      StringBuffer sb=new StringBuffer();
      for (int i = s.length(); i < length; i++) {
         sb.append(zeroChar);
      }
      return (rightSide ? s+sb.toString() : sb.toString()+s);
   }
}